<?php

// verifies the login data on every page in the member's section (this code has to be included on the top of every access-restricted page)

require("include/session.inc");
require("include/functions.inc");

/*
header("Content-Type: text/plain");
foreach (array_keys($_POST) as $k) {
  print "$k = {$_POST[$k]}\n";
}
exit;
*/

if($ime == '') {
	header("location:bloglogin.php?registerime=0&date=".$date);
  	exit;
} else if($email == '') {
	header("location:bloglogin.php?registeremail=0&ime=$ime&date=".$date);
  	exit;
} else if($email != '') {
	if (!eregi("^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+"."@"."[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\."."[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$", $email)) {
		header("location:bloglogin.php?registervalidemail=0&ime=$ime&prezime=$prezime&date=".$date);
  		exit;
	}

	list ($username, $domain) = split("@", $email);

	if (!checkdnsrr($domain.".", "MX")) {
		header("location:bloglogin.php?registervalidemail=0&ime=$ime&prezime=$prezime&date=".$date);
  		exit;
	}
}
if($password == '') {
	header("location:bloglogin.php?registerpassword=0&ime=$ime&prezime=$prezime&email=$email&date=".$date);
  	exit;
}
if($password != $passwordconfirm) {
	header("location:bloglogin.php?registerpasswordconfirm=0&ime=$ime&prezime=$prezime&email=$email&date=".$date);
  	exit;
}
if(!$uslovi) {
	header("location:bloglogin.php?registeruslovi=0&ime=$ime&prezime=$prezime&email=$email&date=".$date);
  	exit;
}

// connect to DB

openConnection();

// user verification

$sql = "SELECT * FROM sunsers WHERE email='".$email."'";
$result = mysql_query($sql)
        or die ("Could not retrieve data!");

if (mysql_num_rows($result) != 1) {
 $sqlr = "insert into sunsers (ime, prezime, email, lozinka, timestamp, status, pismo) values ('".$ime."','".$prezime."','".$email."','".$password."', now(), '1','".$pismo."')";
 $resr = mysql_query($sqlr)
 		or die("Greska vo postavuvanje na podatocite.");

  header("location:bloglogin.php?r=1&date=".$date);
  exit;

} else {
   
  header("location:bloglogin.php?registeremail=postoi&ime=$ime&prezime=$prezime&date=".$date);
  exit;
}

?>